package com.net.wanjian.networkhospitalmanager.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.activity.evaluate.SimpleEvaluateActivity;
import com.net.wanjian.networkhospitalmanager.adapter.base.RecyclerBaseAdapter;
import com.net.wanjian.networkhospitalmanager.bean.SearchEvaluateResult;
import com.net.wanjian.networkhospitalmanager.utils.TimeDateUtils;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description:
 * Created by lidongcai on 17/7/17.
 * Email:  lidongcaiocc@163.com
 */

public class EvaluateEventHomeAdapter extends RecyclerBaseAdapter<SearchEvaluateResult.DataBean.EventListBean, EvaluateEventHomeAdapter.ViewHolder> {

    public EvaluateEventHomeAdapter(Context context) {
        super(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_evaluate_event, parent, false);
        AutoUtils.auto(view);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final SearchEvaluateResult.DataBean.EventListBean item, int position) {
        int simpleCount = Integer.parseInt(URLDecoderUtil.getDecoder(item.getUnEvaluatedSimpleTableCount()));
        int deepCount = Integer.parseInt(URLDecoderUtil.getDecoder(item.getUnEvaluatedDeepTableCount()));
        int count = 0;
        if (simpleCount >= 0 && deepCount >= 0) {
            count = simpleCount + deepCount;
        } else if (simpleCount == -1 && deepCount != -1){
            count = deepCount;
        }else if (simpleCount != -1 && deepCount == -1){
            count = simpleCount;
        }

        //是否显示按钮
        if (count == 0){
            holder.setItemEvaluateEventBtn(false);
        }else {
            holder.setItemEvaluateEventBtn(true);
            holder.setTabUnreadNumTv(count+"");
        }

        holder.setItemEvaluateEventNameTv(URLDecoderUtil.getDecoder(item.getEventName()));

        //设置时间友好显示
        //设置开放到关闭的时间，需要格式化
        String openTime = TimeDateUtils.formatTimeFriend(URLDecoderUtil.getDecoder(item.getEventStartTime()));
        String closeTime = TimeDateUtils.formatTimeFriend(URLDecoderUtil.getDecoder(item.getEventEndTime()));
        String ot = openTime.split(" ")[0];
        String ct = closeTime.split(" ")[0];
        if (ot.equals(ct)) {
            holder.setItemEvaluateEventDateTv(openTime + "—" + closeTime.split(" ")[1]);
        } else {
            holder.setItemEvaluateEventDateTv(openTime + "—" + closeTime);
        }


        holder.itemEvaluateEventBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SimpleEvaluateActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(SimpleEvaluateActivity.SIMPLE_EVALUATE_ACTIVITY_EVENT_ID,item);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_evaluate_event_name_tv)
        TextView itemEvaluateEventNameTv;
        @BindView(R.id.item_evaluate_event_date_tv)
        TextView itemEvaluateEventDateTv;
        @BindView(R.id.item_evaluate_event_btn)
        Button itemEvaluateEventBtn;
        @BindView(R.id.tab_unread_num_tv)
        TextView tabUnreadNumTv;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setItemEvaluateEventNameTv(String itemEvaluateEventNameTv) {
            this.itemEvaluateEventNameTv.setText(itemEvaluateEventNameTv);
        }

        public void setItemEvaluateEventDateTv(String itemEvaluateEventDateTv) {
            this.itemEvaluateEventDateTv.setText(itemEvaluateEventDateTv);
        }

        public void setTabUnreadNumTv(String tabUnreadNumTv) {
            this.tabUnreadNumTv.setText(tabUnreadNumTv);
        }

        /**
         * 设置是否显示去评价
         *
         * @param showButton
         */
        public void setItemEvaluateEventBtn(boolean showButton) {
            if (showButton) {
                this.itemEvaluateEventBtn.setVisibility(View.VISIBLE);
                this.tabUnreadNumTv.setVisibility(View.VISIBLE);
            } else {
                this.itemEvaluateEventBtn.setVisibility(View.GONE);
                this.tabUnreadNumTv.setVisibility(View.GONE);
            }

        }
    }

}
